草庐IT

Java final 与 C++ const

全部标签

c++ - 下标对 const 的引用

我在这里查看一些C++代码,但我不理解某些东西。它无关紧要,但它来自与文档一起提供的YARP(机器人中间件)教程。virtualvoidgetHeader(constBytes&header){constchar*target="HUMANITY";for(inti=0;i现在,header是对const的引用,因此不能在此函数中修改。get是在它上面调用的,它的原型(prototype)是char*get()const;。header.get()如何下标和修改?该程序编译正常。我可能不明白这里发生了什么,但我是基于我在C++Primer中读到的内容......非常感谢您的澄清!祝你有

c++ - 为什么 QObject::disconnect(const QMetaObject::Connection &connection) 采用 const 参数来修改它?

staticboolQObject::disconnect(constQMetaObject::Connection&connection)此方法旨在断开现有的Connection对象以修改它。那么为什么将函数参数声明为const引用?在源码实现(qtbase/src/corelib/kernel/qobject.cpp)中,可以发现不可避免的const_cast:const_cast(connection).d_ptr=0;当函数的目的是修改它时,将函数参数标记为const有什么好处? 最佳答案 原因纯粹是历史原因。最初设想的AP

c++ - 为什么 VS 和 gcc 在这里调用不同的转换运算符(const vs non-const)?

这段代码当然很蠢,但我写它只是为了说明问题。在这里:#includeusingnamespacestd;structfoo{inta=42;templateoperatorT*(){cout(&a);}templateoperatorconstT*()const{cout(&a);}templateTget(){coutoperatorT();}};intmain(){foomyFoo;cout()使用VisualStudio2019(ISOC++17,/Ox)编译时的输出是:Tget()operatorconstT*()const42gcc8.3(-std=c++17,-O3)的输出

c++ - 通过 const 获取参数并通过 const 返回的函数的生命周期延长

在C++中,当你有以下情况时:std::stringget_string();std::stringconst&v=get_string();从get_string()返回的临时对象的生命周期延长为与引用v相同的生命周期;如果我有以下内容:std::stringconst&get_string(std::stringconst&p){returnp;}std::stringconst&v=get_string(std::string{"Hello"});临时的生命周期是否延长?或者这是悬空引用;我的理解是临时绑定(bind)到p的生命周期并且它只在函数的持续时间内存在,并且对临时对象的

c++ - 如何分配一个 std::pair 其中一个组件类型为 const?

我正在尝试编写一个与std::map兼容的关联容器。为此,我必须创建一个插入方法,该方法以std::pair的形式接受一个新项,其中第一个组件是const类型。例如:std::pairp.我遇到的问题是这样的对象不能分配给另一个对象。因此,在我的MapCompatibleContainer的内部代码中,我无法将新对复制到私有(private)变量(std::vector)。我该如何解决这个问题?谢谢 最佳答案 正如您所说,您不能分配给const对象。标准容器通过分配原始内存并就地构造对象来解决这个问题。复制构造仍然有效。此外,关联容

c++ - 字符串隐式转换运算符到 const char*/wchar_t *

我发现MFC/ATLCString类在Win32C++代码中非常方便;特别是我发现我们可以将CString的实例直接传递给Win32API的LPCWSTR(即constwchar_t*)参数很方便,感谢CString定义的隐式转换运算符。相反,当使用std::wstring时,需要显式调用.c_str()方法。那么,为什么STL字符串类(std::string和std::wstring)需要显式方法调用(c_str())而不是定义一个隐式的constchar*/constwchar_t*转换运算符?隐式转换运算符是否隐藏着严重的陷阱? 最佳答案

c++ - 重载 ‘pow(const double&, double)’的调用不明确

我是C++的新手,这是我的问题:我需要这个数量:h=pow(mesh.V()[i0],1.0/3);但是每次编译程序时都会收到此错误消息:callofoverloaded‘pow(constdouble&,double)’isambiguous如果我写doubleV=mesh.V()[i0];h=pow(V,1.0/3);我得到:callofoverloaded‘pow(double&,double)’isambiguous现在我想我明白了constdouble&和double&指的是什么,但是我怎样才能转换constdouble&到double?谢谢! 最

c++ - const 引用在 C++ 中是否有外部链接?

根据C++1998标准第3.5节的第3条,const引用具有内部链接。Anamehavingnamespacescope(3.3.5)hasinternallinkageifitisthenameofanobject,reference,functionorfunctiontemplatethatisexplicitlydeclaredstaticor,anobjectorreferencethatisexplicitlydeclaredconstandneitherexplicitlydeclaredexternnorpreviouslydeclaredtohaveexternall

c++ - 将 const char** 与模板特化结合使用

我正在尝试为返回数值数组的最大值(通用版本)或C字符串数组的最长C字符串(特化)的函数编写模板特化。如果我不使用const-ness,我的函数原型(prototype)看起来像这样templateTmaxn(T*my_Tptr,unsignedintn);templatechar*maxn(char**my_cstrings,unsignedintn);并且我的代码可以编译。但是,如果我尝试使用const-ness,我的函数原型(prototype)将如下所示,templateTmaxn(constT*my_Tptr,unsignedintn);templatechar*maxn(co

c++ - Qt 5 : const char * QString cast

在Qt4中,可以自动将QString转换为“constchar*”,例如我可以将QString传递给需要“constchar*”的函数。voidmyFunction(constchar*parameter);QStringmyString;myFunction(myString);//worksinQT4然而,在Qt5中,我会收到“错误C2440:‘typecast’:无法从‘QString’转换为‘constchar*’”(这是VisualC++2008编译器,其他编译器会抛出类似的东西)。如果我对文档的理解正确,那是因为QT5中不再包含Qt3兼容层。当然,我可以更改函数调用myFu